package org.syncsoft.overalt.gui.impl.window.addFile;

import java.util.Collection;
import java.util.Set;

import org.syncsoft.overalt.common.exception.SystemException;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.document.entity.Permission;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.window.AbstractWindowController;

public class AddFileWindowController extends AbstractWindowController<AddFileWindow> {
	private String filePath;
	
	public AddFileWindowController(String filePath) {
		setFilePath(filePath);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doCreateWindow()
	 */
	@Override
	protected AddFileWindow doCreateWindow() throws SystemException {
		AddFileWindow window = new AddFileWindow(this);
		
		Collection<Community> communities = getCore().getComunities();
		window.setCommunities(communities);
		
		return window;
	}

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.AbstractWindowController#doSubmit(org.syncsoft.overalt.gui.swing.api.ContextWindowValue)
	 */
	@Override
	protected void doSubmit(WindowContextValue context) throws SystemException {
		String title = context.getValue("title");
		String description = context.getValue("description");
		
		String permissionString = context.getValue("permission");
		Permission permission = parsePermission(permissionString);
		
		Collection<Integer> communityCodes = context.getValues("associetedCommunities");
		Set<Community> communities = parseCommunities(communityCodes);
		
		String tagsString = context.getValue("tags");
		Set<Tag> tags = parseTags(tagsString);
		
		getCore().addFile(getFilePath(), title, description, communities, tags, permission);
	}

	//	GETs AND SETs
	private String getFilePath() {
		return filePath;
	}
	private void setFilePath(String filePath) {
		this.filePath = filePath;
	}
}
